/*
 *    Copyright (c) 2018-2025, daoism All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: daoism
 */
package com.mkh.st.admin.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkh.st.admin.api.entity.SysMenu;
import com.mkh.st.admin.api.entity.SysPermissionScope;
import com.mkh.st.admin.api.entity.SysRoleMenu;
import com.mkh.st.admin.api.entity.SysUserRoleScope;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.mapper.SysPermissionScopeMapper;
import com.mkh.st.admin.mapper.SysUserRoleScopeMapper;
import com.mkh.st.admin.service.*;
import com.mkh.st.admin.strategy.BaseScopeStrategy;
import com.mkh.st.admin.strategy.Context;
import com.mkh.st.common.core.constant.enums.DocTypeEnum;
import com.mkh.st.common.security.service.DaoismUser;
import com.mkh.st.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.nio.file.Watchable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色数据权限表
 *
 * @author daoism code generator
 * @date 2021-07-06 09:27:05
 */
@Service
@AllArgsConstructor
public class SysUserRoleScopeServiceImpl extends ServiceImpl<SysUserRoleScopeMapper, SysUserRoleScope> implements SysUserRoleScopeService {

	private final Context context;

	private final SysUserRoleScopeMapper sysUserRoleScopeMapper;

	private final SysPermissionScopeService sysPermissionScopeService;


	@Override
	public List<DataPermissionVO> getPermissionList(Long userId, Long tenantId, List<Long> roleIds) {
		List<DataPermissionVO> list=new ArrayList<>();
		roleIds.forEach(roleId ->{
			List<SysPermissionScope> scopeList = sysPermissionScopeService.list();
			DataPermissionVO vo=new DataPermissionVO();
			vo.setRoleId(roleId);
			for (SysPermissionScope obj:scopeList) {
				SysUserRoleScope scopes = sysUserRoleScopeMapper.selectOne(Wrappers.<SysUserRoleScope>lambdaQuery()
					.eq(SysUserRoleScope::getRoleId, roleId).eq(SysUserRoleScope::getUserId, userId)
					.eq(SysUserRoleScope::getScopeId,obj.getScopeId()));
				context.getScopeStrategy(obj.getScopeId()).setDataPermission(vo,scopes,tenantId);
			}
			list.add(vo);
		});
		return list;
	}

	@Override
	public List<DataPermissionVO> getPermissionListByScopeIds(Long userId, Long tenantId, List<Long> roleIds, List<Long> ids) {
		List<DataPermissionVO> list=new ArrayList<>();
		roleIds.forEach(roleId ->{
			DataPermissionVO vo=new DataPermissionVO();
			vo.setRoleId(roleId);
			ids.forEach(item ->{
				SysUserRoleScope scopes = sysUserRoleScopeMapper.selectOne(Wrappers.<SysUserRoleScope>lambdaQuery()
						.eq(SysUserRoleScope::getRoleId, roleId).eq(SysUserRoleScope::getUserId, userId)
						.eq(SysUserRoleScope::getScopeId,item));
				context.getScopeStrategy(item).setDataPermission(vo,scopes,tenantId);
			});
			list.add(vo);
		});
		return list;
	}
}
